using System;
using System.Collections.Generic;
using System.Text;

namespace Weazel.Microblaze.Simulator.Components.Microblaze.Instructions
{
    /// <summary>
    /// Multiply Immediate
    /// </summary>
    public class Muli : Instruction
    {
        public override int CycleCount
        {
            get { return 1; }
        }

        public Muli()
            : base("muli")
        {
        }

        /// <summary>
        /// Multiplies the contents of registers rA and the value IMM, sign-extended to 
        /// 32 bits; and puts the result in register rD. This is a 32-bit by 32-bit 
        /// multiplication that will produce a 64-bit result. The least significant word 
        /// of this value is placed in rD. The most significant word is discarded.
        /// </summary>    
        public override void Execute(MicroblazeComponent MicroblazeComponent)
        {
            uint ra = MicroblazeComponent.ReadRegister(SourceRegisterA);
            uint imm = GetImmediateValue(MicroblazeComponent);

            ulong result = (ra * imm);

            MicroblazeComponent.WriteRegister(DestinationRegister, (uint)result, Address);
        }

        public override string ToString()
        {
            return String.Format("muli r{0}, r{1}, {2}",
                DestinationRegister,
                SourceRegisterA,
                ImmediateValue
            );
        }
    }
}
